import React,{Component} from "react";
import {Form, Input, message, Modal} from "antd";
import JobService from "../service/JobService";

class JobUpdate extends Component{
    layout = {
        labelCol: {span: 5},
        wrapperCol: {span: 16},
    };

    validateMessages = {
        required: "'${label}'不能为空!",
    };

    // 表单对象
    formRef = React.createRef();
    jobService = new JobService();


    constructor(props) {
        super(props);
        this.state={
            visible:false,
            updateInfo:{}
        }
    }

    componentDidMount() {
        this.props.onRef(this)
    }

    showModal(jobInfo){
        this.setState({
            visible:true,
            updateInfo:jobInfo
        })
    }

    refreshJobList(){
        this.props.refreshJobList()
    }



    /**
     * 取消修改定时任务
     */
    handleUpdateCancel = () => {
        this.setState({
            visible: false,
        });

    }


    /**
     * 确定修改定时任务
     */
    handleUpdateOk = () => {
        this.setState({
            updateInfo:{
                cronExpression:this.formRef.current.getFieldsValue().updateJob.cronExpression
            }
        })
        this.jobService.updateJob(this.state.updateInfo).then(resp => {
            const result = resp.data
            if (result && result.code===0){
                message.success('修改成功');
                this.refreshJobList();
                this.setState({
                    visible: false,
                });
            }else{
                message.error(result.message);
            }
        }).catch(err=>{
            message.error("系统错误，请联系管理员");
        })
    }


    render() {
        return  <Modal
            title="修改任务"
            destroyOnClose={true}
            visible={this.state.visible}
            onOk={this.handleUpdateOk}
            onCancel={this.handleUpdateCancel}
            okText="提交"
            cancelText="取消"
        >
            <Form {...this.layout} ref={this.formRef}
                  validateMessages={this.validateMessages}>
                <Form.Item name={['updateJob', 'cronExpression']} label="cron表达式" rules={[{required: true}]}>
                    <Input/>
                </Form.Item>
            </Form>
        </Modal>
    }
}

export default JobUpdate;